<?php

namespace App\Models\Game\Platform\Org;

use App\Models\Game\Model;
use App\Models\Traits\Game\LogsActivity;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;

class AccessKey extends Model
{
    use LogsActivity, SoftDeletes;

    /**
     * 数据表名称
     *
     * @var string
     */
    protected $table = 't_plat_org_access_keys';

    /**
     * 查询后隐藏的字段
     *
     * @var array
     */
    protected $hidden = [
        'created',
        'updated',
    ];

    /**
     * 不可批量赋值的字段
     *
     * @var array
     */
    protected $guarded = [];

    /**
     * Secret 加盐 key
     *
     * @var string
     */
    protected $salt = 'slo';

    /**
     * 制作 AccessKeyId
     *
     * @return string
     */
    protected function makeAccessKeyId(): string
    {
        return Str::orderedUuid();
    }

    /**
     * 制作 AccessKeySecret
     *
     * @param string $accessKeyId
     * @return string
     */
    protected function makeAccessKeySecret(string $accessKeyId): string
    {
        return strtoupper(sha1($this->salt . Str::random() . $accessKeyId));
    }

    /**
     * 制作 AccessKey
     *
     * @return array
     */
    protected function makeAccessKey()
    {
        return [
            'access_key_id' => $accessKeyId = $this->makeAccessKeyId(),
            'access_key_secret' => $this->makeAccessKeySecret($accessKeyId),
        ];
    }

    /**
     * 制作签名（sha1加密，并转小写）
     *
     * @param string $nonce
     * @param int $timestamp
     * @return string
     */
    public function makeSignature(string $nonce, int $timestamp): string
    {
        return strtolower(sha1($this->access_key_secret . $nonce . $timestamp));
    }
}
